package cn.edu.ctbu.studentmanage.api;

import cn.edu.ctbu.studentmanage.core.PageUtils;
import cn.edu.ctbu.studentmanage.core.RUtil;
import cn.edu.ctbu.studentmanage.domain.Course;
import cn.edu.ctbu.studentmanage.service.CourseService;
import cn.edu.ctbu.studentmanage.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class CourseApiController {

    @Autowired
    private CourseService courseService;


    /**
     * 读取课程./api/courseindex
     * @return
     */
    @GetMapping("/courseindex")
    public R findAll(){
        return RUtil.success(courseService.findAll());
    }

    /**
     * 根据id获取某一门课程/api/courseget/cid
     *
     * @return
     */

    @GetMapping("/courseget/{cid}")
    public R get(@PathVariable Integer cid) {
        Course course = courseService.getByCid(cid);

        return RUtil.success(course);
    }

    /**
     * 新增课程./api/courseinsert
     * @param course
     * @param bindingResult
     * @return
     */
    @PostMapping("/api/courseinsert")
    public R insert(@Valid Course course, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            String errStr = bindingResult.getFieldError().getDefaultMessage();
            log.error(errStr);
        }
        courseService.Insert(course);
        return RUtil.success(null);
//        log.info(course.toString());
    }

    /**
     * 更新课程./api/courseupdate
     * @param course
     */
    @PutMapping ("/courseupdate")
    public R update(Course course){

        courseService.update(course);

        log.info(course.toString());
        return RUtil.success(null);
    }

    /**
     * 根据id删除一门课程./api/coursedelete/cid
     * @param cid
     */
    @DeleteMapping ("/coursedelete/{cid}")
    public R delete(@PathVariable Integer cid){

        courseService.delete(cid);

        return RUtil.success(null);
//        log.info(Course.toString());
    }

    /**
     *
     * 分页读取全部内容./api/getbypagecourse
     * @return
     */
    @GetMapping("/getbypagecourse")
    public R findByPage(@RequestParam(value = "page",defaultValue = "0")Integer page,
                                @RequestParam(value = "size",defaultValue = "10")Integer size,
                                @RequestParam(value = "cname",defaultValue = "")String cname
    ){

        Page<Course> coursePage ;
        if(StringUtils.isEmpty(cname)
        ){
            Pageable pageable = PageRequest.of(page,size, Sort.by("cid"));
            coursePage =courseService.findAll(pageable);
        }else {
            Course course = new Course();
            course.setCname(cname);

            Pageable pageable = PageRequest.of(page,size,Sort.by("cid"));
            ExampleMatcher matcher =ExampleMatcher.matching()
                    .withMatcher("cname",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Course> example = Example.of(course,matcher);
//        Example<Course> example = Example.of(Course);
            coursePage = courseService.findAll(example,pageable);
        }

        PageUtils pageUtils = new PageUtils(coursePage.getContent(),coursePage.getTotalElements());
        return RUtil.success(pageUtils);
    }
}